<?php
require_once 'common/config.php';
require_once 'common/functions.php';

check_login();
$db = db_connect();

// 搜索参数处理
$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$status = isset($_GET['status']) ? intval($_GET['status']) : -1;
$coach_id = isset($_GET['coach_id']) ? intval($_GET['coach_id']) : 0;

// 分页参数
$per_page = 10;
$page = max(1, intval($_GET['page'] ?? 1));
$offset = ($page - 1) * $per_page;

// 构建查询条件
$where = "1=1";
$params = [];

if ($search) {
    $where .= " AND (u.name LIKE :search OR c.name LIKE :search OR e.content LIKE :search)";
    $params[':search'] = "%$search%";
}

if ($status != -1) {
    $where .= " AND e.status = :status";
    $params[':status'] = $status;
}

if ($coach_id > 0) {
    $where .= " AND e.coach_id = :coach_id";
    $params[':coach_id'] = $coach_id;
}

// 获取总记录数
$count_sql = "SELECT COUNT(*) 
             FROM coach_evaluate e
             LEFT JOIN users u ON e.user_id = u.id
             LEFT JOIN coaches c ON e.coach_id = c.id
             WHERE $where";

$count_stmt = $db->prepare($count_sql);
foreach ($params as $k => $v) {
    $count_stmt->bindValue($k, $v);
}
$count_stmt->execute();
$total = $count_stmt->fetchColumn();
$total_pages = ceil($total / $per_page);

// 获取评价数据
$sql = "SELECT e.*, 
               u.name as user_name, 
               u.avatar as user_avatar,
               c.name as coach_name,
               c.avatar as coach_avatar
        FROM coach_evaluate e
        LEFT JOIN users u ON e.user_id = u.id
        LEFT JOIN coaches c ON e.coach_id = c.id
        WHERE $where
        ORDER BY e.create_time DESC
        LIMIT $offset, $per_page";

$stmt = $db->prepare($sql);
foreach ($params as $k => $v) {
    $stmt->bindValue($k, $v);
}
$stmt->execute();
$evaluations = $stmt->fetchAll();

// 状态配置
$status_options = [
    -1 => '全部状态',
    0 => '待审核',
    1 => '已通过',
    2 => '已拒绝'
];

// 获取教练列表
$coaches = $db->query("SELECT id, name FROM coaches")->fetchAll();
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>评价管理 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
    /* 新增自定义样式 */
    .filter-container {
        background: #fff;
        padding: 20px;
        border-radius: 4px;
        box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        margin-bottom: 20px;
    }

    .search-form {
        display: flex;
        gap: 10px;
        align-items: center;
    }

    .search-form .form-group {
        flex: 1;
        margin: 0;
    }

    .user-info-cell {
        display: flex;
        align-items: center;
        gap: 8px;
    }

    .rating-stars {
        color: #f1c40f;
    }

    .content-cell {
        max-width: 400px;
        line-height: 1.6;
        padding: 8px 0;
    }
    </style>
</head>
<body>
    <div class="wrapper">
        <?php include 'includes/sidebar.php'; ?>
        
        <div class="main-content">
            <header class="top-header">
                <div class="header-left">
                    <h2>用户评价管理</h2>
                </div>
            </header>

            <div class="content">
                <!-- 搜索筛选栏 -->
                <div class="filter-container">
                    <form method="GET" class="search-form">
                        <div class="form-group">
                            <input type="text" name="search" 
                                   class="form-control"
                                   placeholder="搜索用户/教练/评价内容"
                                   value="<?= htmlspecialchars($search) ?>">
                        </div>
                        
                        <div class="form-group">
                            <select class="form-control" name="status">
                                <?php foreach ($status_options as $k => $v): ?>
                                <option value="<?= $k ?>" <?= $status == $k ? 'selected' : '' ?>>
                                    <?= $v ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="form-group">
                            <select class="form-control" name="coach_id">
                                <option value="0">所有教练</option>
                                <?php foreach ($coaches as $c): ?>
                                <option value="<?= $c['id'] ?>" <?= $coach_id == $c['id'] ? 'selected' : '' ?>>
                                    <?= htmlspecialchars($c['name']) ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search"></i> 搜索
                        </button>
                    </form>
                </div>

                <!-- 评价列表 -->
                <div class="data-table">
                    <table>
                        <thead>
                            <tr>
                                <th width="5%">ID</th>
                                <th width="15%">用户信息</th>
                                <th width="12%">教练</th>
                                <th width="13%">评分</th>
                                <th width="35%">评价内容</th>
                                <th width="10%">状态</th>
                                <th width="20%">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($evaluations as $eval): ?>
                            <tr>
                                <td><?= $eval['id'] ?></td>
                                <td>
                                    <div class="user-info-cell">
                                        <img src="../api/user/<?= htmlspecialchars($eval['user_avatar']) ?>" 
                                             class="avatar-small"
                                             alt="用户头像">
                                        <?= htmlspecialchars($eval['user_name']) ?>
                                    </div>
                                </td>
                                <td><?= htmlspecialchars($eval['coach_name']) ?></td>
                                <td>
                                    <div class="rating-stars">
                                        <?php for ($i=0; $i<5; $i++): ?>
                                        <i class="fas fa-star <?= $i < $eval['rating'] ? 'active' : '' ?>"></i>
                                        <?php endfor; ?>
                                    </div>
                                </td>
                                <td class="content-cell"><?= nl2br(htmlspecialchars($eval['content'])) ?></td>
                                <td>
                                    <span class="badge badge-<?php 
                                        switch($eval['status']) {
                                            case 0: echo 'secondary'; break;
                                            case 1: echo 'success'; break;
                                            case 2: echo 'danger'; break;
                                            default: echo 'secondary';
                                        }
                                    ?>">
                                        <?= $status_options[$eval['status']] ?? '未知' ?>
                                    </span>
                                </td>
                                <td>
                                    <div class="action-buttons">
                                        <?php if ($eval['status'] != 1): ?>
                                        <button onclick="updateStatus(<?= $eval['id'] ?>, 1)"
                                                class="btn-icon text-success" title="通过">
                                            <i class="fas fa-check-circle"></i>
                                        </button>
                                        <?php endif; ?>
                                        
                                        <?php if ($eval['status'] != 2): ?>
                                        <button onclick="updateStatus(<?= $eval['id'] ?>, 2)"
                                                class="btn-icon text-danger" title="拒绝">
                                            <i class="fas fa-times-circle"></i>
                                        </button>
                                        <?php endif; ?>
                                        
                                        <button onclick="deleteEvaluation(<?= $eval['id'] ?>)"
                                                class="btn-icon text-secondary" title="删除">
                                            <i class="fas fa-trash-alt"></i>
                                        </button>
                                    </div>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <!-- 分页 -->
                <?= generate_pagination($page, $total_pages, $_GET) ?>
            </div>
        </div>
    </div>

    <script>
    function updateStatus(id, status) {
        if (confirm('确定要执行此操作吗？')) {
            fetch(`evaluation_action.php?action=status&id=${id}&status=${status}`)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        location.reload();
                    } else {
                        alert(data.message || '操作失败');
                    }
                });
        }
    }

    function deleteEvaluation(id) {
        if (confirm('确定要删除此评价吗？')) {
            fetch(`evaluation_action.php?action=delete&id=${id}`)
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        location.reload();
                    } else {
                        alert(data.message || '删除失败');
                    }
                });
        }
    }
    </script>

    <style>
    /* 自定义样式补充 */
    .user-cell, .coach-cell {
        display: flex;
        align-items: center;
        gap: 8px;
    }

    .rating {
        color: #ddd;
    }
    .rating .text-warning {
        color: #f1c40f;
    }

    .content-cell {
        max-width: 300px;
        line-height: 1.6;
    }

    .action-buttons {
        display: flex;
        gap: 8px;
    }
    </style>
</body>
</html>